package com.yueqian.xml.mycode;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SAXHandler extends DefaultHandler {
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    // 存放最终结果
    private List<Account> accList = new ArrayList<Account>();
    // 定义当前的Account对象
    private Account nowAcc = null;
    //当前内容
    private String txtContext;

    @Override
    public void startDocument() throws SAXException {
        System.out.println("开始解析文档...");
    }

    @Override
    public void endDocument() throws SAXException {
        System.out.println("结束解析文档...");
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
//		StringBuffer attrs = new StringBuffer();
//		for (int i = 0; i < attributes.getLength(); i++) {
//			attrs.append(attributes.getQName(i)).append("=").append(attributes.getValue(i)).append(",");
//		}
//		
//		System.out.println("开始解析标签："+qName+"\t属性:"+attrs.toString());
        // 如果开始解析acc标签，则建立一个新对象
        if ("acc".contentEquals(qName)) {
            nowAcc = new Account();
        }
        //获取属性
        for (int i = 0; i < attributes.getLength(); i++) {
            if ("gender".equals(attributes.getQName(i))) {
                nowAcc.setGender("男".equals(attributes.getValue(i)));
            } else if ("typeName".equals(attributes.getQName(i))) {
                nowAcc.setType(attributes.getValue(i));
            }
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
//		System.out.println("结束标签解析："+qName);
        // 如果结束acc标签，则将结果添加到列表，并置空对象
        if ("acc".contentEquals(qName)) {
            this.accList.add(nowAcc);
            if (this.accList.size() > 100) {
                //将这100个Account保存到数据库

                //清空这个列表
                this.accList.clear();
            }
            nowAcc = null;
        } else if ("name".equals(qName)) {
            nowAcc.setName(txtContext);
        } else if ("money".equals(qName)) {
            if (!checkNum(txtContext)) {
                throw new IllegalArgumentException();
            }
            nowAcc.setMoney(Double.parseDouble(txtContext));
        } else if ("accDate".contentEquals(qName)) {
            if (!checkDate(txtContext)) {
                throw new IllegalArgumentException();
            }
            nowAcc.setAccDate(parseDate(txtContext));
        }
        //清空内容
        txtContext = null;


    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
//		System.out.println("文本内容："+new String(ch, start, length));
        //记录当前的解析内容
        txtContext = new String(ch, start, length);
    }

    public static boolean checkNum(String numStr) {
        if (numStr == null || numStr.trim().isEmpty()) {
            return false;
        }
        return numStr.matches("\\d*\\.?\\d*");
    }

    public static boolean checkDate(String dateStr) {
        if (dateStr == null || dateStr.trim().isEmpty()) {
            return false;
        }
        return dateStr.matches("\\d{4}\\-\\d{1,2}\\-\\d{1,2}");
    }

    public static Date parseDate(String dateStr) {

        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Account> getAccList() {
        return accList;
    }


}
